<template>
  <div class="page_box">
    <div class="top_box">
      <div class="name flex">
        <img :src="detail.icon||'https://imgage.maimaihao.com/yuduhulian/yudoudefault.png'" alt="">
        <div>
          <div class="name_time flex">
            <b>{{detail.groupName}}</b>
            <div class="endtime" v-if="detail.businessId">
              <span v-if="detail.buyWay==2">到期时间：{{detail.closeTime}}</span>
            </div>
          </div>
          <div class="tip">
            {{ detail.jianjie ||'暂无简介...'}}
          </div>
        </div>

      </div>
      <div class="btns">

        <el-button v-if="!detail.businessId" size="small" type="primary" @click="kaitong" v-loading="loading">
          <span v-if="detail.buyWay==1">免费</span>
          <span v-if="detail.buyWay==2">{{detail.monthAmount}}虞豆/月</span>
          <span v-if="detail.buyWay==3">{{detail.monthAmount}}虞豆/长期</span>
          丨立即订购
        </el-button>
        <el-button v-if="(detail.buyWay==2&&detail.businessId)||(detail.buyWay!=2&&detail.businessId==5492&&detail.state!=1)" size="small" type="primary" @click="submitForm" v-loading="loading">立即续费</el-button>
        <el-button style="background-color: #24A81333;border-color: #24A81333;color: #24A813;" v-else-if="detail.buyWay!=2&&detail.businessId" size="small" type="primary" disabled>正在使用</el-button>
        <el-button style="margin-right:10px" icon="el-icon-chat-dot-round" size="small" plain type="primary" @click="lianxi()">联系客服</el-button>
      </div>
      <div class="imgs_box " v-if="imgArr&&imgArr.length">
        <swiper :options="swiperOptions">
          <swiper-slide v-for="item in imgArr" :key="item.id">
            <img class="imgitem" :src="item.url" alt="">
          </swiper-slide>

        </swiper>
        <div class="swiper-prev">
          <i class="el-icon-back"></i>
        </div>
        <div class="swiper-next">
          <i class="el-icon-right"></i>
        </div>
      </div>
    </div>
    <div class="bot_box">
      <div v-if="detail.video">
        <div class="title">使用教程</div>
        <div class="video_box">
          <div class="video">
            <video controls :src="detail.video"></video>
          </div>
        </div>
      </div>
      <div>
        <div class="title">功能介绍</div>
        <!-- <div>
          <img src="https://p3.img.cctvpic.com/photoworkspace/contentimg/2025/03/20/2025032011222426009.png" preview="0" alt="">
        </div> -->
        <div class="rich_box" v-html="richText" v-if="richText">

        </div>
        <div v-else>
          <el-empty description="暂无介绍"></el-empty>
        </div>
      </div>

    </div>
    <lianxiwomen ref="lianxi"></lianxiwomen>
    <moduleOpenDialog :tenantId="shopInfo.tenantId" :campNumber="shopInfo.campNumber" ref="moduleOpenDialog" @success="getGroupDetail"></moduleOpenDialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getMenuGroupDetail, updateJieShao } from "@/api/system/menuGroup.js";
import { Swiper, SwiperSlide } from "vue-awesome-swiper";
import "swiper/css/swiper.css";
// 开通弹窗
import moduleOpenDialog from "../../../components/tenant/moduleOpenDialog.vue";
import valueAddedServices from "@/api/shanghuduan/valueAddedServices";
import lianxiwomen from "../../../components/ConsignmentFourzero/lianxiwomen.vue";


export default {
  components: { Swiper, SwiperSlide, moduleOpenDialog, lianxiwomen },
  computed: {
    ...mapGetters(["shopInfo"]),
  },
  data() {
    return {
      loading: false,
      groupId: "",
      detail: {},
      imgArr: [],
      swiperOptions: {
        loop: true, // 循环模式选项
        slidesPerView: 3.5,
        speed: 1000,
        autoplay: {
          delay: 2000, // 自动切换时间间隔
          disableOnInteraction: true, // 用户操作后是否继续自动切换
        },
        pagination: false,
        navigation: {
          nextEl: ".swiper-next", // 自定义下一页按钮的选择器
          prevEl: ".swiper-prev", // 自定义上一页按钮的选择器
        },
      },
      serviceList: [],
      richText: "",
      richImgs: [],

      photoPreview:null
    };
  },
  created() {
    this.groupId = this.$route.query.groupId;
    this.getGroupDetail();
    // var _this = this
    // window.previewImg = function(i) {
    //     _this.picturePreview(i)
    // }
  },

  methods: {
    getGroupDetail() {
      this.richImgs = [];
      this.richText = "";
      getMenuGroupDetail(this.groupId).then((res) => {
        this.detail = { ...res.data };
        if (this.detail.xqImg) {
          this.imgArr = JSON.parse(this.detail.xqImg);
        }
        if (this.detail.xiangqing) {
          this.handleHtml(this.detail.xiangqing);
        }
        this.getMyService();
      });
    },
    handleHtml(rich) {
      // 使用正则表达式匹配所有图片
      let imgReg = /<img.*?(?:>|\/>)/gi; //匹配图片中的img标签
      let match = rich.match(imgReg); //筛选出所有的img
      if (match) {
        // 循环图片数组
        this.richImgs = [];
        for (let i = 0; i < match.length; i++) {
          const item = match[i];
          // 取图片url保存
          const regstr = /src="([^"]*)"/;
          this.richImgs.push(item.match(regstr));
          const len = item.length;
          const _str = item.slice(0, len - 1) + `preview='0' class='preview-image' />`; // 追加 preview 之后的img
          rich = rich.replace(item, _str);
        }
      }
      this.richText = rich;
      this.$nextTick(() => {
        // this.$previewRefresh();
    
      });
    },
    //  // 图片预览
    // picturePreview(i) {
    //   console.log("%c Line:154 🍓 i", "color:#33a5ff", i);
    //   this.$viewerApi({
    //     images: this.richImgs,
    //     options: {
    //       initialViewIndex: i,
    //     }
    //   });
    // },

    // 获取租户开通的增值服务
    getMyService() {
      valueAddedServices
        .myValueAddedServiceList({
          dataType: "all",
          type: "1",
          tenantId: this.shopInfo.tenantId,
        })
        .then((res) => {
          if (res.rows && res.rows.length) {
            this.serviceList = res.rows;
          } else {
            this.serviceList = [];
          }
          this.mergeServices();
        });
    },
    // 合并获取到的已开通服务和全部服务列表
    mergeServices() {
      if (this.serviceList && this.serviceList.length) {
        this.serviceList.forEach((r2) => {
          if (this.detail.groupId == r2.businessId) {
            this.detail = { ...this.detail, ...r2 };
          }
        });
      }
    },

    // 开通功能模块服务
    kaitong() {
      let type = 1;
      if (this.detail.serveType) type = this.detail.serveType;
      this.$refs.moduleOpenDialog.open(this.detail, 1, type);
    },
    // 续费
    submitForm() {
      let type = 1;
      if (this.detail.serveType) {
        type = this.detail.serveType;
        this.detail.groupId = this.detail.businessId;
      }
      this.$refs.moduleOpenDialog.open(this.detail, 2, type);
    },
    lianxi() {
      this.$refs.lianxi.open();
    },
  },
};
</script>

<style lang="scss" scoped>
.top_box {
  width: 1309px;
  background: #ffffff;
  box-shadow: 0px 0px 4px 0px rgba(202, 202, 202, 0.5);
  border-radius: 10px;
  padding: 25px;
}
.bot_box {
  width: 1309px;
  background: #ffffff;
  box-shadow: 0px 0px 4px 0px rgba(202, 202, 202, 0.5);
  border-radius: 10px;
  padding: 20px;
  margin-top: 10px;
  margin-bottom: 20px;
  .title {
    font-weight: 500;
    font-size: 16px;
  }

  .video_box {
    background: rgba(208, 203, 246, 0.3);
    // filter: blur(50px);
    border: 1px solid #d8b6e8;
    border-radius: 8px;
    margin-top: 15px;
    margin-bottom: 15px;
    padding: 10px;
    .video {
      width: 600px;
      height: 338px;
      margin: 0 auto;
      video {
        width: 100%;
        height: 100%;
      }
    }
  }
}
.name {
  align-items: center;
  img {
    width: 48px;
    height: 48px;
    margin-right: 10px;
  }
  .name_time {
    align-items: center;
    font-size: 16px;
    .endtime {
      font-size: 12px;
      color: #999;
      margin-left: 10px;
    }
  }
  .tip {
    width: 280px;
    font-weight: 500;
    font-size: 14px;
    color: #666666;
    line-height: 20px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    margin-top: 2px;
  }
}
.btns {
  margin-top: 10px;
}
.imgs_box {
  margin-top: 15px;
  position: relative;
  width: 1269px;
  background: linear-gradient(138deg, #f4f5fa 0%, #f7f8fc 100%);
  border-radius: 4px;
  padding: 15px;
  .imgitem {
    width: 340px;
    height: 250px;
  }
}

.swiper-prev,
.swiper-next {
  text-align: center;
  line-height: 48px;
  font-size: 24px;
  color: #6c5dd3;
  width: 48px;
  height: 48px;
  background: #ffffff;
  box-shadow: 0px 2px 4px 2px rgba(164, 164, 164, 0.5);
  border-radius: 50%;
  position: absolute;
  top: 110px;
  z-index: 1;
  cursor: pointer;
}
.swiper-prev {
  left: -5px;
}
.swiper-next {
  right: -5px;
}
::v-deep .rich_box img{
  max-width: 100%;
}
</style>